package Study_6;

/**
 * @author: zxy
 * @date: 2024/5/17 18:33
 * @version: 1.0
 */
public class CustomerList {

    private Customer[] customers; // 保存客户对象的数组
    private int total = 0;        // 记录已保存客户对象的数量


    //构造器
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    /**
     * 添加客户
     * @param customer 要添加的客户
     * @return true or false
     */
    public boolean addCustomer(Customer customer){
        if (total >= 0 && total < 10) {
            Customer cust = new Customer();
            cust = customer;
            customers[total] = cust;
            total++;
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 将新客户替换客户列表中的老客户
     * @param index 所要替换的老客户的索引
     * @param cust  新客户
     * @return true or false
     */
    public boolean replaceCustomer(int index, Customer cust){
        if (index >= 0 && index < total) {
            customers[index] = cust;
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 删除索引指向的客户
     * @param index 要删除客户的索引
     * @return true or false
     */
    public boolean deleteCustomer(int index){
        if (index >= 0 && index < total) {
            for (int i = index; i < total - 1; i++) {
                customers[i] = customers[i + 1];
            }
            customers[total - 1] = null;
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 返回已添加的所有客户
     * 创建一个新数组，长度为已添加用户的个数，将原数组复制给新数组，返回新数组
     * @return 已添加的所有客户
     */
    public Customer[] getAllCustomers(){
        Customer[] custs = new Customer[total];
        for (int i = 0; i < custs.length; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }


    public Customer getCustomer(int index){
        return customers[index];
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}
